package de.hochschuletrier.bachelor.antfarm.world;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import de.hochschuletrier.bachelor.antfarm.constants.Constants;
import de.hochschuletrier.bachelor.antfarm.entity.Entity;
import de.hochschuletrier.bachelor.antfarm.entity.Foodsource;
import de.hochschuletrier.bachelor.antfarm.entity.Pheromon;
import de.hochschuletrier.bachelor.antfarm.settings.Settings;

public class World {
	private ArrayList<Entity> entities;
	private ArrayList<Pheromon> pheromons;
	private Map map;
	private static World instance;
	private int timeElapsed;
	
	private Entity selectedEntity;

	public World() {
		instance = this;

		entities = new ArrayList<Entity>();
		pheromons = new ArrayList<Pheromon>();
		map = new Map();

		timeElapsed = 0;
	}

	public static World getInstance() {
		return instance;
	}

	public void update(GameContainer gc, int delta) {
		timeElapsed += delta;
		if (Settings.getCalculatedFramesPerSecond() > 0) {
			float neededTime = 1f / Settings.getCalculatedFramesPerSecond();

			if (neededTime < (timeElapsed / 1000f)) {
				internalUpdate(gc, delta);
				for (int i = 0; i < entities.size(); ++i) {
					entities.get(i).update(gc, delta);
				}
				timeElapsed -= neededTime * 1000f;
			}
		}
		else {
			timeElapsed = 0;
		}
	}
	
	private void internalUpdate(GameContainer gc, int delta) {
		if (Math.random() <= Settings.getChanceForNewFoodsource()) {
			map.createNewRandomFoodsource();
		}
	}

	public void render(Graphics g, GameContainer gc) {
		map.render(g, gc);

		for (Entity currentEntity : entities) {
			if (currentEntity instanceof Pheromon)
				currentEntity.render(g, gc);
		}

		for (Entity currentEntity : entities) {
			if (!(currentEntity instanceof Pheromon)) {
				currentEntity.render(g, gc);
				if (currentEntity == selectedEntity) {
					g.setColor(Color.red);
					g.drawOval(currentEntity.getX() * Constants.TILESIZE, currentEntity.getY() * Constants.TILESIZE, Constants.TILESIZE, Constants.TILESIZE);
				}
			}
		}
	}

	public void putPheromon(int x, int y) {
		if (x >= 0 && y >= 0 && x < map.getWidth() && y < map.getHeight())
		pheromons.add(new Pheromon(x, y));
	}

	public int getAmountOfPheromons(int x, int y) {
		int amount = 0;
		for (Pheromon p : pheromons) {
			if (p.getX() == x && p.getY() == y) {
				amount++;
			}
		}
		return amount;
	}

	public int getTileHeight(int x, int y) {
		return map.getTileHeight(x, y);
	}

	public ArrayList<Entity> getEntities() {
		return new ArrayList<Entity>(entities);
	}

	public void addEntity(Entity e) {
		entities.add(e);
	}

	public void removeEntity(Entity e) {
		entities.remove(e);
	}

	public Map getMap() {
		return map;
	}

	public ArrayList<Foodsource> getFoodsources() {
		ArrayList<Foodsource> list = new ArrayList<Foodsource>();
		for (Entity e : getEntities()) {
			if (e instanceof Foodsource) {
				list.add((Foodsource) e);
			}
		}

		return list;
	}

	public Entity getSelectedEntity() {
		return selectedEntity;
	}

	public void setSelectedEntity(Entity selectedEntity) {
		this.selectedEntity = selectedEntity;
	}
}
